package io.renren.modules.problem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.problem.dao.ProblemMapper;
import io.renren.modules.problem.entity.Problem;
import io.renren.modules.problem.service.ProblemService;
import io.renren.modules.sys.entity.SysRoleEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class ProblemServiceImpl  implements ProblemService {
    @Autowired
    ProblemMapper problemMapper;

    /**
     * 新增问题
     * @param problem
     */
    @Override
    public void saveProblem(Problem problem) {
        //数据的检验
        if(problem==null|| StringUtils.isBlank(problem.getCode())|| StringUtils.isBlank(problem.getTitle())||StringUtils.isBlank(problem.getContent())){
            throw new RuntimeException("数据缺失");
        }
        //进行数据的保存
        problemMapper.insert(problem);
    }

    /**
     * 删除问题
     * @param roleIds
     */

    @Override
    public void deleteBatch(Long[] roleIds) {
        // 删除问题
        problemMapper.deleteBatchIds(Arrays.asList(roleIds));
    }

    /**
     * 修改问题
     * @param problem
     */
    @Override
    public void update(Problem problem) {
        problemMapper.updateById(problem);
    }

    /**
     * 进行分页查询列表
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String title = (String) params.get("title");
        LambdaQueryWrapper<Problem> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(title)){
            wrapper.eq(Problem::getTitle,title);
        }
        IPage<Problem> page = problemMapper.selectPage(new Query<Problem>().getPage(params),wrapper);

        return new PageUtils(page);
    }
}
